# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from string import upper
from openerp.addons.dm_rubylong import get_rubylong_fields_xml, get_rublong_data_url
from openerp.addons.dm_base.utils import field_selection_name as selection_name
from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name

#会计凭证
class account_move(osv.osv):
    
    _inherit="account.move"
    
    def _get_rubylong_xml(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
#                        #公司地址
                        ('company_id.street','company_id_street'),
                        ('company_id.street2','company_id_street2'),
                        ('company_id.city','company_id_city'),
                        ('company_id.state_id.name','company_id_state'),
                        ('company_id.country_id.name','company_id_country_name'),
                        #电话,传真
                        ('company_id.phone','company_id_phone'),
                        ('company_id.fax','company_id_fax'),
                        
                        #会计凭证
                        ('name','accounting_number'),
                        #凭证薄
                        ('journal_id.name','accounting_book'),
                        #会计期间
                        ('period_id.name','accounting_period'),
                        #日期
                        ('date','accounting_date'),
                        #备注
                        ('narration','note'),
                        ('amount', 'amount_total'),
                        ('amount', 'amount_debit_total'),
                        ('amount', 'amount_credit_total'),
                        
                        ('id','order_id'),
                        ]
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #detail data
            line_fields = [
                        #业务日期
                        ('date_biz','date'),
                        #名称
                        'name',
                        #业务伙伴
                        ('partner_id.name','partner'),
                        #借方
                        'debit',
                        #贷方                        
                        'credit',
                        #金额
                        'amount_currency',
#                        #状态
#                        ('state','status'),
#                        #对账
#                        ('reconcile_id.name','reconcile'),
                        
                        ('move_id.id','order_id'),  
                        ]
            
            for line in order.line_id:
                #科目
                line_fields_new = line_fields + [{'account':'%s - %s'%(line.account_id.code, line.account_id.name)}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)
            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)

        return res            

    _columns = {
            'rubylong_xml_file':fields.function(_get_rubylong_xml, type='text', string='Rubylong xml'),
            }
    
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO    